var Rand = {
    rndInRange: function(lowVal, upVal, isNumber) {
        if (typeof lowVal === "number" && typeof upVal === "number") {
            var choices = (isNumber ? (upVal - lowVal + 1) :
                (upVal - lowVal)),
                rnd = Math.random() * choices + lowVal;
            return isNumber ? Math.floor(rnd) : rnd;
        } else {
            console.log("rndInRange方法异常：传入的参数必须为number类型。");
        }
    },

    unique: function(input, oldArr) {
        // reference:http://www.sharejs.com/tutorial/tutorial/9654

        for (var i = oldArr.length - 1; i >= 0; i--) {
            if (oldArr[i] === input) {
                return oldArr;
            }
        }

        oldArr.push(input);
        return oldArr;
    },

    contains: function(input, oldArr){
        for (var i = oldArr.length - 1; i >= 0; i--) {
            if(oldArr[i] === input){
                return true;
            }
        }
        return false;
    },

    generateXNum: function(options) {
        var lowVal = options.lowVal,
            upVal = options.upVal,
            x = options.x,
            isRepeat = options.isRepeat,
            isNumber = options.isNumber;
        var reArr = [],
            hash = {};

        if (isRepeat) {
            for (var i = x; i > 0; i--) {
                reArr.push(Rand.rndInRange(lowVal, upVal, isNumber));
            }
        } else {
            // when upVal - lowVal = x maybe endless loop will be happened
            /*while (reArr.length !== x) {
                var rnd = Rand.rndInRange(lowVal, upVal, isNumber);
                reArr = Rand.unique(rnd, reArr);
            }*/

            // init array and splice array maybe have a bad performance
            // and you can not generate a random float number. 
            /*var tempArr = new array(x);
            for (var i = x - 1; i >= 0; i--) {
                tempArr.push(lowVal++);         
            };
            for (var i = x - 1; i >= 0; i--) {
                var tempIndex = tempArr[Rand.rndInRange(0,x-1,true)];
                reArr.push(tempIndex);
                // make sure the random no duplicate
                tempArr.splice(tempIndex,1);        
            };*/

            var loopTime = upVal - x + 1;
            for (var i = loopTime; i <= upVal; i++) {
                var tempRnd = Rand.rndInRange(lowVal, i, isNumber);
                if (!Rand.contains(tempRnd, reArr)) {
                    reArr.push(tempRnd);
                }
                else{
                    reArr.push(i);
                }  
            };
        }

        return reArr;
    }
};

console.log(Rand.generateXNum({
    lowVal: 1,
    upVal: 100,
    x: 10,
    isRepeat: false,
    isNumber: true
}));
console.log(Rand.generateXNum({
    lowVal: 1,
    upVal: 100,
    x: 10,
    isRepeat: false,
    isNumber: false
}));
console.log(Rand.generateXNum({
    lowVal: 1,
    upVal: 100,
    x: 10,
    isRepeat: true,
    isNumber: true
}));
console.log(Rand.generateXNum({
    lowVal: 1,
    upVal: 100,
    x: 10,
    isRepeat: true,
    isNumber: false
}));